﻿#region Using Statements
using System;
using GameBase.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.ScreenComponents;
#endregion

namespace StarFox.Testing
{
    /// <summary>
    /// Immplementation of a frame rate counter.
    /// <para>Use this class for testing purposes only.</para>
    /// </summary>
    public class FrameRateCounter : ScreenComponent2D
    {
        #region Fields

        SpriteFont font;

        int frameRate;
        int frameCounter;
        TimeSpan elapsedTime;

        #endregion

        #region Screen Transition

        /// <summary>
        /// Makes the ScreenComponent2D transition to visible state.
        /// </summary>
        public override void Show()
        {
        }

        /// <summary>
        /// Makes the ScreenComponent2D transition to hidden state.
        /// </summary>
        public override void Hide()
        {
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent2D is associated to.</param>
        public FrameRateCounter(Screen screen)
            : base(screen)
        {
            font = Game.Content.Load<SpriteFont>(ContentSettings.GetAsset("MenuSmallFont"));
        }

        #endregion

        #region Update

        /// <summary>
        /// Called when the ScreenComponent2D needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when the ScreenComponent2D needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Draw(GameTime gameTime)
        {
            frameCounter++;

            string text = frameRate.ToString() + " FPS";
            Vector2 position = new Vector2(Screen.Width * 0.65f, Screen.Height * 0.20f);

            SpriteBatch.DrawString(font, text, position, Color.Fuchsia);
        }

        #endregion
    }
}
